package swing;

import cz.muni.fi.pv168.ContactManager.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Adam Zvak, 374358
 * Date: 15.5.12
 * Time: 0:52
 * To change this template use File | Settings | File Templates.
 */
public class MainWindow extends JFrame implements ActionListener {
    private PersonManagerImpl personManager = new PersonManagerImpl();
    private ContactManagerImpl contactManager = new ContactManagerImpl();
    private AddressBookManagerImpl addressBookManager = new AddressBookManagerImpl();

/*    private JTable personTable;
    private DefaultTableModel personTableModel;
    private JScrollPane personScrollPane;
    private String[] personTableColumns;
    private String[][] personsTableRows;

    private JTable contactTable;
    private DefaultTableModel contactTableModel;
    private JScrollPane contactScrollPane;
    private String[] contactTableColumns;
    private String[][] contactTableRows;*/

    private List<Person> persons;
    private ListModel<Person> personsModel;
    private JList<Person> personList;

    private List<Contact> contacts;
    private ListModel<Contact> contactsModel;
    private JList<Contact> contactList;

    private JTextField searchTextField;
    private JButton searchButton;

    private JButton addButton;
    private JButton editButton;
    private JButton delButton;

    private Person selectedPerson;
    private Contact selectedContact;
    String selected = null;

    private void createUIComponents() {
        //PersonList
        persons = personManager.findAllPersons();
        personsModel = new DefaultListModel<Person>(persons.toArray());
        personList = new JList<Person>(personsModel);
        personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //ContactList
        contacts = null;
        contactList = new JList<Contact>(contacts);
        contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //Policko vyhledavani
        searchTextField = new JTextField("", 30);
        searchTextField.setHorizontalAlignment(JTextField.LEFT);

        //Tlacitko vyhledat
        searchButton = new JButton("Search");
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        //Pridat kontakt/osobu
        addButton = new JButton("Add");            //prida bud kontakt nebo osobu (podle posledniho kliku na tabulku)
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(selected.equals("person")){
                    AddPerson addPerson = new AddPerson();
                    addPerson.setSize(300, 200);
                    addPerson.setVisible(true);
                }
                if(selected.equals("contact") && selectedPerson!=null){
                    AddContact addContact = new AddContact(selectedPerson);
                    addContact.setSize(300, 200);
                    addContact.setVisible(true);
                }
            }
        });

        //Editovat kontakt/osobu
        editButton = new JButton("Edit");   //
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(selected.equals("person") && selectedPerson != null){
                    AddPerson editPerson = new AddPerson(selectedPerson);
                    editPerson.setSize(300, 200);
                    editPerson.setVisible(true);
                }
                if(selected.equals("contact") && selectedContact != null && selectedPerson != null){
                    AddContact editContact = new AddContact(selectedContact);
                    editContact.setSize(300, 200);
                    editContact.setVisible(true);
                }
            }
        });

        //Smazat kontakt/osobu
        delButton = new JButton("Del");
        delButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });

    }


    public MainWindow(){

        personManager.setDataSource(DBUtil.getDataSource());
        contactManager.setDataSource(DBUtil.getDataSource());
        addressBookManager.setDataSource(DBUtil.getDataSource());

        createUIComponents();
        createMainWindow();
    }

    private void createMainWindow() {
        setTitle("Contact Manager");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container content = getContentPane();
        FlowLayout mainLayout = new FlowLayout();
        content.setLayout(mainLayout);

        Container searchPane = new Container();
        searchPane.setLayout(new FlowLayout());
        content.add(searchPane);
        searchPane.add(searchTextField);
        searchPane.add(searchButton);

        Container tablesPane = new Container();
        tablesPane.setLayout(new FlowLayout());
        content.add(tablesPane);
        tablesPane.add(personList);
        tablesPane.add(contactList);

        Container buttonsPane = new Container();
        buttonsPane.setLayout(new FlowLayout());
        content.add(buttonsPane);
        buttonsPane.add(addButton);
        buttonsPane.add(editButton);
        buttonsPane.add(delButton);

        setContentPane(content);
        Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenDimensions.width/4,screenDimensions.height/4 );
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

}


